package com.shop.cloud.upms.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cloud.common.core.constant.CacheConstants;
import com.shop.cloud.common.core.constant.CommonConstants;
import com.shop.cloud.common.data.tenant.TenantContextHolder;
import com.shop.cloud.upms.common.entity.H5PortalDomain;
import com.shop.cloud.upms.admin.mapper.H5PortalDomainMapper;
import com.shop.cloud.upms.admin.service.H5PortalDomainService;
import com.shop.cloud.upms.admin.properties.H5portalDomainProperties;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;

/**
 * H5前端域名
 *
 * @author
 * @date 2022-12-14 11:03:21
 */
@Service
@AllArgsConstructor
public class H5PortalDomainServiceImpl extends ServiceImpl<H5PortalDomainMapper, H5PortalDomain> implements H5PortalDomainService {

	private final H5portalDomainProperties h5portalDomainProperties;
	private final RedisTemplate redisTemplate;

	@Override
	@Cacheable(value = CacheConstants.H5PORTAL_DOMAIN_CACHE, key = "#id", unless = "#result == null")
	public H5PortalDomain getById(Serializable id) {
		return super.getById(id);
	}

	@Override
	@CacheEvict(value = CacheConstants.H5PORTAL_DOMAIN_CACHE, key = "#entity.getId()")
	public boolean updateById(H5PortalDomain entity) {
		return super.updateById(entity);
	}

	@Override
	@CacheEvict(value = CacheConstants.H5PORTAL_DOMAIN_CACHE, key = "#id")
	public boolean removeById(Serializable id) {
		return super.removeById(id);
	}

	@Override
	@CacheEvict(value = CacheConstants.H5PORTAL_DOMAIN_CACHE, key = "#updateWrapper.getEntity().getId()")
	public boolean update(Wrapper<H5PortalDomain> updateWrapper) {
		return super.update(updateWrapper);
	}

	@Override
	public void createDefault(H5PortalDomain h5PortalDomain) {
		String id = StrUtil.format("{}.{}", TenantContextHolder.getTenantId(), h5portalDomainProperties.getDomain(h5PortalDomain.getType()));
		h5PortalDomain.setTenantId(TenantContextHolder.getTenantId());
		h5PortalDomain.setIsDefault(CommonConstants.YES);
		h5PortalDomain.setId(id);
		//查询是否已有默认域名
		H5PortalDomain h5PortalDomain2 = this.getOne(Wrappers.<H5PortalDomain>query().lambda()
				.eq(H5PortalDomain::getIsDefault,CommonConstants.YES)
				.eq(H5PortalDomain::getTenantId,TenantContextHolder.getTenantId())
				.eq(H5PortalDomain::getType,h5PortalDomain.getType()));
		if(h5PortalDomain2 != null){//已有默认域名，且基础域名有变化，则删除已有的默认域名并重新添加
			if(!id.equals(h5PortalDomain2.getId())){
				redisTemplate.delete(StrUtil.format("{}::{}", CacheConstants.H5PORTAL_DOMAIN_CACHE, h5PortalDomain2.getId()));
				baseMapper.deleteById(h5PortalDomain2.getId());
				baseMapper.insert(h5PortalDomain);
			}
		}else{
			//没有默认域名，则直接添加
			baseMapper.insert(h5PortalDomain);
		}
	}
}
